﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.Page;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.Database.Service;

namespace T.FAS.Runtime.Page.Dao
{
    public class PageEventDao : DaoService<PageEvent>
    {
        public PageEventDao() :
           base()
        {
            DBName = "TFAS";
        }

        public override string TableName => "FAS_PageEvent";
        public override string IDColumnName => "PageID";
        public override List<string> ExceptColumnName => new List<string> { "Parameters", "ComponentID", "Params", "DataSource" };
        public override Dictionary<string, object> GetDataMapping(PageEvent data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("ActionType");
            datas.Add("ActionType", (int)(data.ActionType));
            datas.Remove("ActionSourceType");
            datas.Add("ActionSourceType", (int)(data.ActionSourceType));
            return datas;
        }
        public override PageEvent ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("ActionType");
            myCol.Remove("ActionSourceType");
            var data = base.ConvertToObject(myCol, row);
            if (row["ActionType"] != DBNull.Value)
                data.ActionType = (ActionType)Enum.Parse(typeof(ActionType),
                                                Convert.ToString(row["ActionType"]));
            if (row["ActionSourceType"] != DBNull.Value)
                data.ActionSourceType = (ActionSourceType)Enum.Parse(typeof(ActionSourceType),
                                                Convert.ToString(row["ActionSourceType"]));
            return data;
        }
        public List<PageEvent> GetPageEvents(string pageID)
        {
            var result = new List<PageEvent>();
            List<object> objects = base.GetObjectsByFilter(string.Format("PageID='{0}'", pageID));
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as PageEvent));
            }
            return result;
        }
    }
}
